package com.itboye.ihomebank.activity.qianyue;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.itboye.ihomebank.MainActivity;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.activity.login.LoginActivity;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.base.MyApplcation;
import com.itboye.ihomebank.bean.RedDotBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.activity.hetong.ActivityTianXieHeTongThree;
import com.itboye.ihomebank.presenter.HousePresenter;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.controller.LoginController;
import com.itboye.ihomebank.util.controller.UnLoginState;

import java.util.HashMap;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;

/**
 * 资产清单
 */
public class ZiChanQingDanActivity extends BaseOtherActivity implements Observer{
    View v_statusbar;
    ImageView close_icon;// 返回图片
    TextView add_shap_title_tv;// 标题
    EditText et_dian,et_shui,et_meiqi;

    LinearLayout layout_zichan;//资产清单
    TextView tv_zichan;//资产清单
    TextView qingdan_ok;
    UserPresenter userPresenter;
    HousePresenter mHousePresenter;
    String uid;
    String contractNo;
    String selectDevice;
    @Override
    protected int layout_id() {
        return R.layout.activity_zi_chan_qing_dan;
    }

    @Override
    protected void onInitController() {

    }

    @Override
    protected void onInit() {
        setStatusBarHeight(v_statusbar);
        add_shap_title_tv.setText("填写附件单");
        userPresenter=new UserPresenter(this);
        mHousePresenter = new HousePresenter(this);
        uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
        contractNo = getIntent().getStringExtra("contractNo");
    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.close_icon:
                finish();
                break;

            case R.id.tv_zichan:
            case R.id.layout_zichan:
                Intent intent = new Intent(ZiChanQingDanActivity.this,ActivityTianXieHeTongThree.class);
                startActivityForResult(intent, 101);
                break;
            case R.id.qingdan_ok:
               //EditText et_dian,et_shui,et_meiqi;
                String dian=et_dian.getText().toString();
                String shui=et_shui.getText().toString();
                String meiqi=et_meiqi.getText().toString();
                Gson gson = new Gson();
                Map<String, String> map = new HashMap<String, String>();
                map.put("electric_instrument_reading", dian);
                map.put("water_meter_reading", shui);
                map.put("gas_reading", meiqi);
                map.put("facility", selectDevice);
                String jsonStr = gson.toJson(map);
                showProgressDialog("正在提交,请稍后...",true);
                userPresenter.tiJiaoHeTong(uid, contractNo, jsonStr);
                System.out.println("》》生成的json字符串" + jsonStr);
                break;
            default:
                break;
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 101 && resultCode == 102) {
            if (data != null) {
                selectDevice = data.getStringExtra("selectDevice");
                tv_zichan.setText(selectDevice);
            }
        }
    }

    @Override
    public void update(Observable o, Object arg) {
        ResultEntity entity = handlerError(arg);
        if (entity != null) {
            if (entity.getEventType() == UserPresenter.heTongTiJiao_success) {
                ByAlert.alert("成功");
                //判断红点是否消失
                mHousePresenter.queryRedDot(uid, MyApplcation.device_token);
                finish();
            }
            if (entity.getEventType() == UserPresenter.heTongTiJiao_fail) {
                ByAlert.alert("失败");
            } if (entity.getEventType() == HousePresenter.queryRedDot_success){
                Log.d("main==","main_true");
                RedDotBean mRedDotBean= (RedDotBean) entity.getData();
                if (mRedDotBean.getNeed_write_facility_count()>0){
                    SPUtils.put(this,null,SPContants.FUJIAN_ORDER,true);
                }else {
                    SPUtils.put(this,null,SPContants.FUJIAN_ORDER,false);
                }
                sendBroadcast(new Intent("showRedDot"));
            }else if (entity.getEventType() == HousePresenter.queryRedDot_fail){

            }
        }
        try {
            closeProgressDialog();
        }catch (Exception e){

        }
    }
}
